package com.svs.generator.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.velocity.VelocityContext;

import com.svs.common.config.Global;
import com.svs.common.constant.Constants;
import com.svs.common.support.Convert;
import com.svs.common.utils.DateUtils;
import com.svs.common.utils.StringUtils;
import com.svs.generator.domain.BizTable;
import com.svs.generator.domain.ColumnInfo;
import com.svs.generator.domain.TableInfo;

/**
 * 代码生成器 工具类
 * 
 * @author dujianqiao
 */
public class GenUtils {
	/** 项目空间路径 */
	private static final String PROJECT_PATH = "main/java/com/svs";

	/** mybatis空间路径 */
	private static final String MYBATIS_PATH = "main/resources/mybatis";

	/** html空间路径 */
	private static final String TEMPLATES_PATH = "main/resources/templates";

	/** 类型转换 */
	public static Map<String, String> javaTypeMap = new HashMap<String, String>();

	/**
	 * 设置列信息
	 */
	public static List<ColumnInfo> transColums(List<ColumnInfo> columns) {
		// 列信息
		List<ColumnInfo> columsList = new ArrayList<>();
		for (ColumnInfo column : columns) {
			// 列名转换成Java属性名
			String attrName = StringUtils.convertToCamelCase(column.getColumnName());
			column.setAttrName(attrName);
			column.setAttrname(StringUtils.uncapitalize(attrName));

			// 列的数据类型，转换成Java类型
			String attrType = javaTypeMap.get(column.getDataType());
			column.setAttrType(attrType);

			columsList.add(column);
		}
		return columsList;
	}

	/**
	 * 获取模板信息
	 * 
	 * @return 模板列表
	 */
	public static VelocityContext getVelocityContext(TableInfo table) {
		// java对象数据传递到模板文件vm
		VelocityContext velocityContext = new VelocityContext();
		String packageName = Global.getPackageName();
		velocityContext.put("tableName", table.getTableName());
		velocityContext.put("tableComment", replaceKeyword(table.getTableComment()));
		velocityContext.put("primaryKey", table.getPrimaryKey());
		velocityContext.put("className", table.getClassName());
		velocityContext.put("classname", table.getClassname());
		velocityContext.put("moduleName", GenUtils.getModuleName(packageName));
		velocityContext.put("columns", table.getColumns());
		velocityContext.put("package", packageName);
		velocityContext.put("author", Global.getAuthor());
		velocityContext.put("datetime", DateUtils.getDate());
		return velocityContext;
	}
	
	
	public static VelocityContext getVelocityContext(BizTable table) {
		VelocityContext velocityContext = new VelocityContext();
		String packageName = table.getPackageName() ;
		velocityContext.put("currTable", table);
		velocityContext.put("tableName", table.getName());
		velocityContext.put("tableComment", replaceKeyword(table.getComments()));
		velocityContext.put("primaryKey", table.getPrimaryKey());
		velocityContext.put("className", table.getEntityName());
		velocityContext.put("classname", StringUtils.uncapitalize(table.getEntityName()));
		velocityContext.put("moduleName", GenUtils.getModuleName(packageName));
		velocityContext.put("columns", table.getColumns());
		velocityContext.put("package", packageName);
		velocityContext.put("author", table.getAuthor());
		velocityContext.put("datetime", DateUtils.getDate());
		return velocityContext;
	}
	

	/**
	 * 获取模板信息
	 * 
	 * @return 模板列表
	 */
	public static List<String> getTemplates() {
		List<String> templates = new ArrayList<String>();
		templates.add("vm/java/domain.java.vm");
		templates.add("vm/java/Dao.java.vm");
		templates.add("vm/java/Service.java.vm");
		templates.add("vm/java/Controller.java.vm");
		templates.add("vm/xml/Mapper.xml.vm");
		templates.add("vm/html/list.html.vm");
		templates.add("vm/html/add.html.vm");
		templates.add("vm/html/edit.html.vm");
		templates.add("vm/sql/sql.vm");
		return templates;
	}
	
	
	/**
	 * 获取模板信息
	 * 
	 * @return 模板列表
	 */
	public static List<String> getNewTemplates() {
		List<String> templates = new ArrayList<String>();
		templates.add("vn/java/domain.java.vm");
		templates.add("vn/java/Dao.java.vm");
		templates.add("vn/java/Service.java.vm");
		templates.add("vn/java/Controller.java.vm");
		templates.add("vn/xml/Mapper.xml.vm");
		templates.add("vn/html/list.html.vm");
		templates.add("vn/html/add.html.vm");
		templates.add("vn/html/edit.html.vm");
		templates.add("vn/html/importExcel.html.vm");
		templates.add("vn/sql/sql.vm");
		return templates;
	}
	

	/**
	 * 表名转换成Java类名
	 */
	public static String tableToJava(String tableName) {
		if (Constants.AUTO_REOMVE_PRE.equals(Global.getAutoRemovePre())) {
			tableName = tableName.substring(tableName.indexOf("_") + 1);
		}
		if (StringUtils.isNotEmpty(Global.getTablePrefix())) {
			tableName = tableName.replace(Global.getTablePrefix(), "");
		}
		return StringUtils.convertToCamelCase(tableName);
	}

	/**
	 * 获取文件名
	 */
	public static String getFileName(String template, TableInfo table, String moduleName) {
		// 小写类名
		String classname = table.getClassname();
		// 大写类名
		String className = table.getClassName();
		String javaPath = PROJECT_PATH + "/" + moduleName + "/";
		String mybatisPath = MYBATIS_PATH + "/" + moduleName + "/" + className;
		String htmlPath = TEMPLATES_PATH + "/" + moduleName + "/" + classname;

		if (template.contains("domain.java.vm")) {
			return javaPath + "domain" + "/" + className + ".java";
		}

		if (template.contains("Dao.java.vm")) {
			return javaPath + "mapper" + "/" + className + "Dao.java";
		}

		if (template.contains("Service.java.vm")) {
			return javaPath + "service" + "/" + className + "Service.java";
		}

		if (template.contains("Controller.java.vm")) {
			return javaPath + "controller" + "/" + className + "Controller.java";
		}

		if (template.contains("Mapper.xml.vm")) {
			return mybatisPath + "Mapper.xml";
		}

		if (template.contains("list.html.vm")) {
			return htmlPath + "/" + classname + "List.html";
		}
		if (template.contains("add.html.vm")) {
			return htmlPath + "/" + "add" + className + ".html";
		}
		if (template.contains("edit.html.vm")) {
			return htmlPath + "/" + "edit" + className + ".html";
		}
		if (template.contains("sql.vm")) {
			return classname + "Menu.sql";
		}
		return null;
	}
	
	
	
	/**
	 * 获取文件名
	 */
	public static String getNewFileName(String template, BizTable table, String moduleName) {
		// 大写类名
		String className = table.getEntityName() ;
		// 小写类名
		String classname = StringUtils.uncapitalize(table.getEntityName());
		String javaPath = PROJECT_PATH + "/" + moduleName + "/";
		String mybatisPath = MYBATIS_PATH + "/" + moduleName + "/" + className;
		String htmlPath = TEMPLATES_PATH + "/" + moduleName + "/" + classname;

		if (template.contains("domain.java.vm")) {
			return javaPath + "domain" + "/" + className + ".java";
		}

		if (template.contains("Dao.java.vm")) {
			return javaPath + "mapper" + "/" + className + "Dao.java";
		}

		if (template.contains("Service.java.vm")) {
			return javaPath + "service" + "/" + className + "Service.java";
		}

		if (template.contains("Controller.java.vm")) {
			return javaPath + "controller" + "/" + className + "Controller.java";
		}

		if (template.contains("Mapper.xml.vm")) {
			return mybatisPath + "Mapper.xml";
		}

		if (template.contains("list.html.vm")) {
			return htmlPath + "/" + classname + "List.html";
		}
		if (template.contains("add.html.vm")) {
			return htmlPath + "/" + "add" + className + ".html";
		}
		if (template.contains("edit.html.vm")) {
			return htmlPath + "/" + "edit" + className + ".html";
		}
		if (template.contains("importExcel.html.vm")) {
			return htmlPath + "/" + "import" + className + ".html";
		}
		if (template.contains("sql.vm")) {
			return classname + "Menu.sql";
		}
		return null;
	}

	/**
	 * 获取模块名
	 * 
	 * @param packageName
	 *            包名
	 * @return 模块名
	 */
	public static String getModuleName(String packageName) {
		int lastIndex = packageName.lastIndexOf(".");
		int nameLength = packageName.length();
		String moduleName = StringUtils.substring(packageName, lastIndex + 1, nameLength);
		return moduleName;
	}

	public static String replaceKeyword(String keyword) {
		String keyName = keyword.replaceAll("(?:表|信息)", "");
		return keyName;
	}

	static {
		javaTypeMap.put("tinyint", "Integer");
		javaTypeMap.put("smallint", "Integer");
		javaTypeMap.put("mediumint", "Integer");
		javaTypeMap.put("int", "Integer");
		javaTypeMap.put("integer", "Integer");
		javaTypeMap.put("bigint", "Long");
		javaTypeMap.put("float", "Float");
		javaTypeMap.put("double", "Double");
		javaTypeMap.put("decimal", "BigDecimal");
		javaTypeMap.put("bit", "Boolean");
		javaTypeMap.put("char", "String");
		javaTypeMap.put("varchar", "String");
		javaTypeMap.put("tinytext", "String");
		javaTypeMap.put("text", "String");
		javaTypeMap.put("mediumtext", "String");
		javaTypeMap.put("longtext", "String");
		javaTypeMap.put("time", "Date");
		javaTypeMap.put("date", "Date");
		javaTypeMap.put("datetime", "Date");
		javaTypeMap.put("timestamp", "Date");
	}

	/**
	 * 
	 * @param colType
	 * @return
	 */
	public static Integer counJDBCLen(String colType) {
		Integer result = null ;
		String coLen = "" ;
		if(colType.contains("(") && colType.contains(")")) {
			coLen = colType.substring(colType.indexOf("(")+1,colType.indexOf(")")) ;
			result = Convert.toInt(coLen) ;
		}
		return result ;
	}
}
